$(function () {
    let id = sessionStorage.getItem("groupId");
    findByGroupName(id);
});

function findByGroupName(id) {
    let result = myAjax('/g/group/findMemberByGroupId', 'post', {id: id});
    let html = null;
    if (result.data.length === 0) {
        html += '<option value="0">暂无成员</option>'
        $("#add-member").html('<input type="button" value="添加成员？" ' +
            'style="width: 80px; border-radius: 5px;background-color: darkcyan" onclick="addNewMember(' + id + ')">')
    } else if (result.data.length < 10) {
        html += "<option value='0'>请选择组长</option>";
        for (let i = 0; i < result.data.length; i++) {
            html += '     <option value="' + result.data[i].id + '">' + result.data[i].userName + '</option>';
        }
        $("#add-member").html('<input type="button" value="添加成员？" ' +
            'style="width: 80px; border-radius: 5px;' +
            'background-color: darkcyan" onclick="addNewMember(' + id + ')">');
    } else {
        html += "<option value='0'>请选择组长</option>";
        for (let i = 0; i < result.length; i++) {
            html += '     <option value="' + result[i].id + '">' + result[i].userName + '</option>';
        }
    }
    $("#leader").html(html);
    for (let i = 0; i < result.length; i++) {
        if (result[i].isLeader == 1) {
            //判断如果isleader等于1，则此人就是该组的组长，然后赋值给下拉菜单，
            $('#leader option').filter(function () {
                return $(this).text() == result[i].userName;
            }).prop("selected", true);
        }
    }
}

layui.use("layer");

//弹出添加成员的页面层，并把小组名称赋值给title，方便保存数据获得小组名称
function addNewMember(id) {
    // let result = myAjax('/user/findNoGroupUser','post',{});
    sessionStorage.setItem("id-add-member", id);
    let name = $("#name").val();
    sessionStorage.setItem("add-new-group-member", name);
    let html = "<span id='add-new-group-member'>" + name + "</span>"
    layer.open({
        type: 2,
        anim: 3,
        title: '' + html + '添加成员',
        area: ['400px', '400px'],
        shadeClose: true, //点击遮罩关闭
        content: ['/html/addMember.html', 'no'],
    })
}
